<template>
  <div>
    <el-dialog
      :visible.sync="isVisible"
      title="添加角色"
      center
      width="500px"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <el-form
        size="mini"
        status-icon
        label-width="50px"
        :rules="formRules"
        ref="form"
        :model="form"
      >
        <el-form-item label="角色" prop="name">
          <el-input v-model="form.name" autocomplete="off" placeholder="请输入角色" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" autocomplete="off" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="启用" :value="0" />
            <el-option label="停用" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isVisible = false">取 消</el-button>
        <el-button
          type="primary"
          size="mini"
          :loading="loading"
          @click="insertHandle('form')"
        >
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'RoleAdd',
    props: {
      visible: {
        type: Boolean,
        default: false
      }
    },
    computed: {
      isVisible: {
        get() {
          return this.visible
        },
        set(val) {
          this.$emit('update:visible', val)
        }
      }
    },
    watch: {
      isVisible(val) {
        if (val) {
          this.form = {}
        }
      }
    },
    data() {
      return {
        form: {
          name: '',
          description: '',
          status: ''
        },
        formRules: {
          name: [
            { required: true, message: '请输入角色', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '请输入备注', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '请选择状态', trigger: 'blur' }
          ]
        },
        loading: false
      }
    },
    methods: {
      insertHandle(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // params
            const params = new URLSearchParams()
            params.append('name', this.form.name)
            params.append('description', this.form.description)
            params.append('status', this.form.status)
            // ajax
            this.loading = true
            this.AjaxUtils.post('/role/insertRole', params, true).then((response) => {
              if (response.code === 0) {
                this.loading = false
                this.isVisible = false
                this.$emit('reloadTable')
              }
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .el-select {
    width: 100%;
  }
</style>
